package crazyboyfeng.autodroid.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import crazyboyfeng.autodroid.table.TaskTable;
import crazyboyfeng.autodroid.utils.GlobalContext;

class DBHelper extends SQLiteOpenHelper {
	
    private static DBHelper singleton = null;

    private static final String DATABASE_NAME = "timetask.db";
    private static final int DATABASE_VERSION = 4;
    
    public synchronized static DBHelper getInstance() {
        if (singleton == null) {
            singleton = new DBHelper(GlobalContext.getInstance());
        }
        return singleton;
    }
    
    DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    static final String CREATE_TASK_TABLE_SQL = "create table " + TaskTable.TABLE_NAME
            + "("
            + TaskTable.COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + TaskTable.COLUMN_NAME_NAME + " text,"
            + TaskTable.COLUMN_NAME_DATE + " text,"
            + TaskTable.COLUMN_NAME_TIME + " text,"
            + TaskTable.COLUMN_NAME_TYPE + " text,"
            + TaskTable.COLUMN_NAME_TASK + " text,"
            + TaskTable.COLUMN_NAME_STATE+ " text,"
            + TaskTable.COLUMN_NAME_ACTIVE + " text"
            + ");";
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TASK_TABLE_SQL);
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TaskTable.TABLE_NAME);
        onCreate(db);
    }
}